<?php
/**
* $Id: tv_Controller.php 2942 2012-02-18 10:48:18Z minh.nguyen$
* Base controller for QuinStreet Projects
*/
class TV_Controller extends CI_Controller
{
    /**
    * @desc Name of module the controller belong to
    *
    * @var mixed
    */
    public $module = '';

    /**
     * @desc array to paser data from quoiti
     */

    public $parser = array();

    /**
    * @desc array to hold view data
    */
    protected $data = array();

    /**
    * layout elements of full HTML page to be renderred
    *
    * @uses See AppConstants for constants used by layout
    * @var mixed
    */
    protected $layout = array();

    /**
    * Meta header information
    *
    * @var mixed
    */
    protected $meta = array();

    /**
    * Page template
    *
    * @var mixed
    */
    protected $template = '';

    /**
    * Authorization page, show to user when he is required to provide information for required priviledge
    */
    protected $authUrl = '';

    //Flag to know if controller is processing an ajax request
    protected $isAjax = 0;


    // For PHP4 compatibility
    function TV_Controller() {
        $this->__construct();
        register_shutdown_function(array(&$this, '__destruct'));
    }

    function __construct() {
        parent::__construct();
    }

    function __destruct() {
        // SQL query logging
        if($this->config->item('log_sql') == TRUE) {
            $this->hq_db->log_queries($this->config->item('log_sql_type'));
        }
    }
    /**
    * Hooks
    */
    function pre_execute()
    {
        //The method being execute
        global $method;
        //Check if user has role to access the requested method
        $this->checkAccess($method);
    }

    function post_execute()
    {             
        //if (!array_key_exists(AppContants::LAYOUT_MAINVIEW, $this->layout) || $this->layout[AppContants::LAYOUT_MAINVIEW] == null) { return; }
        $this->data['meta'] =  $this->meta;
        $this->data['layout'] =  $this->layout;
        $this->data['parser'] = $this->parser;                 
        $this->load->view('templates/'.$this->template, $this->data);
    }

    function display_override(){
        //not render the layout, helpful for debuging or ajax request
        if ($this->isAjax){
            # create an instance of Services_JSON
            require_once(BASEPATH . 'application/libraries/json.php');
            $json = new Services_JSON();
            echo $json->encode($this->data);
            return;
        }else{
            global $OUT;
            $OUT->_display();
        }
    }
    /**
    * Check user has role to access a method
    *
    * @uses In oder to have this authorization work, the controller must define a function
    * called accessRules() which return an array of 'method_name' => 'role,list'
    * Be careful with the role list in which role name cannot contain space
    *
    * @param mixed $method
    */
    function checkAccess($method){
        if (method_exists($this, 'accessRules'))
        {
            $rules = $this->accessRules();
            if (array_key_exists($method, $rules))
                $roles = $rules[$method];
            elseif(array_key_exists('*', $rules))
                $roles = $rules['*'];

            $roles = explode(',', $roles);
            $allowRoles = array();
            $deniedRoles = array();
            foreach($roles as $role){
                $role = trim($role);
                if (substr($role,0,1) == '-')
                    $deniedRoles[] = substr($role,1);
                elseif(!empty($role))
                    $allowRoles[] = $role;
            }

            $userRoles = $this->user->roles;
            //Check if user has role that is denied to perform action first
            if (count($deniedRoles) > 0 && is_array($userRoles) && count(array_intersect($deniedRoles, $userRoles)) > 0){
                //User can not access the method
                //As we don't have mean to stop CI core from calling the action here we will redirect user
                $this->user->setRedirectUrl($this->uri->uri_string());
                redirect($this->authUrl);
            }

            if(count($allowRoles) > 0)
            {
                if (!is_array($userRoles) || count(array_intersect($allowRoles, $userRoles)) <= 0)
                {
                    //User can not access the method
                    //As we don't have mean to stop CI core from calling the action here we will redirect user
                    $this->user->setRedirectUrl($this->uri->uri_string());
                    redirect($this->authUrl);
                }
            }
        }
    }
}
?>
